package com.example.secondhandshop.model;

import android.util.Log;


import com.example.secondhandshop.bean.ApiAllGoodsData1;
import com.example.secondhandshop.bean.Item1;
import com.example.secondhandshop.bean.ResponseBody;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SoldRequest {
    private static final String BASE_URL = "https://api-store.openguet.cn/api/member/tran/";
    private static final String APP_ID = "767864940d8644968bb2184c83ae34a4";
    private static final String APP_SECRET = "640848a3d0fd5427e4fa5819a10663d9e2c28";
    private OkHttpClient client = new OkHttpClient();

    public void getItems(int userId, Integer current, Integer size, String keyword, String typeId, OnProductsFetchedListener listener) {
        // 构建查询参数
        StringBuilder urlBuilder = new StringBuilder(BASE_URL + "trading/sell?userId=" + userId);
        if (current != null) {
            urlBuilder.append("&current=").append(current);
        }
        if (size != null) {
            urlBuilder.append("&size=").append(size);
        }
        Log.d("ItemModel", "请求的URL: " + urlBuilder);

        // 创建请求
        Request request = new Request.Builder()
                .url(urlBuilder.toString())
                .get()
                .addHeader("appId", APP_ID)
                .addHeader("appSecret", APP_SECRET)
                .build();

        // 发送请求
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                listener.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.e("ItemModel", "onResponse: " + response);

                if (response.isSuccessful()) {
                    String apiResponseBody = response.body().string();
                    Log.d("ItemModel2", "API Response Body: " + apiResponseBody);
                    Gson gson = new Gson();

                    // 使用 TypeToken 明确指定解析的对象类型
                    //TODO 请求成功处理

                    ResponseBody<ApiAllGoodsData1> responseBody = gson.fromJson(apiResponseBody, new TypeToken<ResponseBody<ApiAllGoodsData1>>(){}.getType());

                    // 检查响应代码
                    if (responseBody.getCode() == 200) {
                        ApiAllGoodsData1 apiAllGoodsData = responseBody.getData(); // 直接获取 Data
                        List<Item1> items = apiAllGoodsData.getRecords(); // 获取产品记录
                        listener.onSuccess(items);
                    } else {
                        listener.onFailure(responseBody.getMsg()); // 使用实际消息
                    }
                } else {
                    listener.onFailure(response.message()); // 捕获请求失败的情况
                }
            }
        });
    }

    // 回调接口定义
    public interface OnProductsFetchedListener {
        void onSuccess(List<Item1> items);
        void onFailure(String error);
    }
}